<?php
class keu_setup_report_item_model extends MY_Model {
	
	function SWReportType()
	{
		$ls_sql	= "	SELECT	kd_report value,
							nama_report display
					FROM	keu_setup_report
					ORDER BY nama_report";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
	
	function KeuCOA($id_item='%')
	{
		$ls_sql	= "	SELECT	ID_COA value,
							ID_COA||' '||NAMA_COA display
					FROM	KEU_COA
					WHERE ID_COA NOT IN (SELECT ID_COA FROM KEU_SETUP_REPORT_ITEM_COA WHERE id_item LIKE '".$id_item."' )
					ORDER BY ID_COA";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
	
	function KeuITEMCOA($id_item='')
	{
		$ls_sql	= "	SELECT	ID_COA value,
							ID_COA||' '||NAMA_COA display
					FROM	KEU_COA
					WHERE ID_COA IN (SELECT ID_COA FROM KEU_SETUP_REPORT_ITEM_COA WHERE id_item = '".$id_item."' )
					ORDER BY ID_COA";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	* 
					FROM	keu_setup_report_item 
					WHERE	(kd_report = '".post('kd_report')."')
					ORDER BY ".(post('sort')=='' ? 'id_item' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetComboGridData()
	{
		$ls_sql	= "	SELECT *
				  FROM (SELECT ROWNUM row_num,
							   '' id_item,
							   NULL id_item_induk,
							   NULL kd_report,
							   NULL no_urut,
							   'NULL' nama_item,
							   NULL status_penomoran,
							   NULL create_user,
							   NULL create_date,
							   NULL modify_user,
							   NULL modify_date
						  FROM DUAL
						UNION ALL
						SELECT ROWNUM row_num,
							   id_item,
							   id_item_induk,
							   kd_report,
							   no_urut,
							   nama_item,
							   status_penomoran,
							   create_user,
							   create_date,
							   modify_user,
							   modify_date
						  FROM (  SELECT *
									FROM keu_setup_report_item
								   WHERE (id_item LIKE '%".post('q')."%'
							OR nama_item LIKE '%".post('q')."%')
								ORDER BY id_item) z) d
				 WHERE row_num BETWEEN 0 AND 10";
					
		return $this->db->query($ls_sql)->result_array();
	}
	
	function GetDetailJSON($id_item='')
	{
		$ls_sql	= "	SELECT	KEU_COA.*
					FROM	KEU_SETUP_REPORT_ITEM_COA A INNER JOIN KEU_COA ON KEU_COA.ID_COA = A.ID_COA
					WHERE A.ID_ITEM = '".$id_item."'
					ORDER BY KEU_COA.ID_COA";
		$result	= $this->db->query($ls_sql);			
		return json_encode( $this->paging($ls_sql) );
	}
	
	
	function AddItem($arr_data){
		$this->db->insert('keu_setup_report_item', $arr_data);
	}
	
	function UpdateItem($arr_data, $arr_item){
		$this->db->update('keu_setup_report_item', $arr_data, $arr_item);
	}
	
	function GetItemSingle($as_id){
		$result = $this->db->get_where('keu_setup_report_item', array('id_item' => $as_id));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	
	function GetReportType($as_report){
		$result = $this->db->get_where('gl_setup_report', array('kd_report' => $as_report));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function DeleteItemSingle($as_id)
	{
		try {
		
			$this->db->delete('keu_setup_report_item_coa', array('id_item' => $as_id));
			$this->db->delete('keu_setup_report_item', array('id_item' => $as_id));
			
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
	
	function AddCoa($arr_data){
		$this->db->insert('keu_setup_report_item_coa', $arr_data);
	}
	
	function UpdateCoa($arr_data, $arr_item){
		$this->db->update('keu_setup_report_item_coa', $arr_data, $arr_item);
	}
	
	
	function DeleteItemCoa($as_id)
	{
		$this->db->delete('keu_setup_report_item_coa', array('id_item' => $as_id));
	}
		
	function GetReportDataLabaRugi($as_report_code, $adt_from='', $as_project_code='%', $as_office_code='%', $as_activity_code='%'){
		$adt_from = ($adt_from=='' ? date('d/m/Y') : $adt_from);
		$adt_from = date_format3($adt_from, 'YYYY-MM-DD');
		
		$ls_sql	= "	SELECT
					keu_setup_report_item.*,(
						SELECT
							sum(pm_project_budget.amount)
						FROM
							pm_project,
							pm_project_budget,
							keu_setup_report_item_coa icoa
						WHERE
							pm_project_budget.code_coa = icoa.id_coa
							AND pm_project.project_code = pm_project_budget.project_code
							AND icoa.id_item = keu_setup_report_item.id_item
							AND pm_project_budget.project_code like '".$as_project_code."'
							AND pm_project.code_office like '".$as_office_code."'
							AND pm_project.code_project_type like '".$as_activity_code."'
					)rka,
					(
						SELECT
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo
						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH(sysdate())
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH(sysdate())
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."') 
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity  like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						AND icoa.id_item = keu_setup_report_item.id_item
					)realisasi,
					(SELECT COUNT(1) FROM keu_setup_report_item tmp WHERE tmp.id_item_induk = keu_setup_report_item.id_item) numchild,
					(SELECT COUNT(1) FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item) item_coa,
					0 level
				FROM
					keu_setup_report_item
				WHERE
					kd_report = '".$as_report_code."' AND
					IFNULL(id_item_induk,'') = ''
				ORDER BY
					no_urut, id_item";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubDataLabaRugi($as_report_code, $arr_data[$i]['id_item'], $arr_data[$i]['level'], $adt_from,$as_project_code,$as_office_code,$as_activity_code));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
	
	function GetReportSubDataLabaRugi($as_report_code, $id_item_induk='', $ai_level=0, $adt_from='', $as_project_code='%', $as_office_code='%', $as_activity_code='%'){
		if($id_item_induk=='') return NULL;
		$adt_from = ($adt_from=='' ? date('d/m/Y') : $adt_from);
		$adt_from = date_format3($adt_from, 'YYYY-MM-DD');
		
		$ls_sql	= "SELECT
					keu_setup_report_item.*,(
						SELECT
							sum(pm_project_budget.amount)
						FROM
							pm_project,
							pm_project_budget,
							keu_setup_report_item_coa icoa
						WHERE
							pm_project_budget.code_coa = icoa.id_coa
							AND pm_project.project_code = pm_project_budget.project_code
							AND icoa.id_item = keu_setup_report_item.id_item
							AND pm_project_budget.project_code like '".$as_project_code."'
							AND pm_project.code_office like '".$as_office_code."'
							AND pm_project.code_project_type like '".$as_activity_code."'
					)rka,
					(
						SELECT
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo
						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH(sysdate())
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH(sysdate())
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."') 
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						AND icoa.id_item = keu_setup_report_item.id_item
					)realisasi,
					(SELECT COUNT(1) FROM keu_setup_report_item tmp WHERE tmp.id_item_induk = keu_setup_report_item.id_item) numchild,
					(SELECT COUNT(1) FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item) item_coa,
					".($ai_level+1)." level
				FROM
					keu_setup_report_item
				WHERE
					kd_report = '".$as_report_code."' AND
					id_item_induk = '".$id_item_induk."'
				ORDER BY
					no_urut, id_item";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubDataLabaRugi($as_report_code, $arr_data[$i]['id_item'], $arr_data[$i]['level'], $adt_from,$as_project_code,$as_office_code,$as_activity_code));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
	
	function GetReportDataNeraca($as_report_code, $adt_from='', $as_project_code='%', $as_office_code='%', $as_activity_code='%'){
		$adt_from = ($adt_from=='' ? date('d/m/Y') : $adt_from);
		$adt_from = date_format3($adt_from, 'YYYY-MM-DD');
		
		$ls_sql	= "	SELECT
					keu_setup_report_item.*,(
						SELECT
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo
						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."')-1
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						AND icoa.id_item = keu_setup_report_item.id_item
					)rka,
					(
						SELECT
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo
						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."')
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						AND icoa.id_item = keu_setup_report_item.id_item
					)realisasi,
					(SELECT COUNT(1) FROM keu_setup_report_item tmp WHERE tmp.id_item_induk = keu_setup_report_item.id_item) numchild,
					(SELECT COUNT(1) FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item) item_coa,
					0 level
				FROM
					keu_setup_report_item
				WHERE
					kd_report = '".$as_report_code."' AND
					IFNULL(id_item_induk,'') = ''
				ORDER BY
					no_urut, id_item";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubDataNeraca($as_report_code, $arr_data[$i]['id_item'], $arr_data[$i]['level'], $adt_from,$as_project_code,$as_office_code,$as_activity_code));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
	
	function GetReportSubDataNeraca($as_report_code, $id_item_induk='', $ai_level=0, $adt_from='', $as_project_code='%', $as_office_code='%', $as_activity_code='%'){
		if($id_item_induk=='') return NULL;
		
		$adt_from = ($adt_from=='' ? date('d/m/Y') : $adt_from);
		$adt_from = date_format3($adt_from, 'YYYY-MM-DD');
		
		$ls_sql	= "	SELECT
					keu_setup_report_item.*,(
						SELECT
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo
						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."')-1
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						AND icoa.id_item = keu_setup_report_item.id_item
					)rka,
					(
						SELECT
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo
						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."')
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						AND icoa.id_item = keu_setup_report_item.id_item
					)realisasi,
					(SELECT COUNT(1) FROM keu_setup_report_item tmp WHERE tmp.id_item_induk = keu_setup_report_item.id_item) numchild,
					(SELECT COUNT(1) FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item) item_coa,
					".($ai_level+1)." level
				FROM
					keu_setup_report_item
				WHERE
					kd_report = '".$as_report_code."' AND
					id_item_induk = '".$id_item_induk."'
				ORDER BY
					no_urut, id_item";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubDataNeraca($as_report_code, $arr_data[$i]['id_item'], $arr_data[$i]['level'], $adt_from,$as_project_code,$as_office_code,$as_activity_code));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
	
	
	
	
	//=========================================
	function GetReportDataArusKas($as_report_code, $adt_from='', $as_project_code='%', $as_office_code='%', $as_activity_code='%'){
		$adt_from = ($adt_from=='' ? date('d/m/Y') : $adt_from);
		$adt_from = date_format3($adt_from, 'YYYY-MM-DD');
		$ls_sql	= "	SELECT
					keu_setup_report_item.*,
					(
						SELECT
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo
						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."')
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						AND icoa.id_item = keu_setup_report_item.id_item
					)realisasi,
					(SELECT COUNT(1) FROM keu_setup_report_item tmp WHERE tmp.id_item_induk = keu_setup_report_item.id_item) numchild,
					(SELECT right(tmp.id_coa,3) FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item  limit 0,1 ) item_coa,
					(SELECT COUNT(1) CNT FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item) gcoa,
					
					0 level
				FROM
					keu_setup_report_item
				WHERE
					kd_report = '".$as_report_code."' AND
					IFNULL(id_item_induk,'') = ''
				ORDER BY
					no_urut, id_item";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubDataArusKas($as_report_code, $arr_data[$i]['id_item'], $arr_data[$i]['level'], $adt_from,$as_project_code,$as_office_code,$as_activity_code));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
	
	function GetReportSubDataArusKas($as_report_code, $id_item_induk='', $ai_level=0, $adt_from='', $as_project_code='%', $as_office_code='%', $as_activity_code='%'){
		if($id_item_induk=='') return NULL;
		$adt_from = ($adt_from=='' ? date('d/m/Y') : $adt_from);
		$adt_from = date_format3($adt_from, 'YYYY-MM-DD');
		$ls_sql	= "	SELECT
					keu_setup_report_item.*,
					(
						SELECT
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo
						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."')
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						AND icoa.id_item = keu_setup_report_item.id_item
					)realisasi,
					(SELECT COUNT(1) FROM keu_setup_report_item tmp WHERE tmp.id_item_induk = keu_setup_report_item.id_item) numchild,
					(SELECT RIGHT(tmp.id_coa,3) FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item  limit 0,1 ) item_coa,
					(SELECT COUNT(1) CNT FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item) gcoa,
					".($ai_level+1)." level
				FROM
					keu_setup_report_item
				WHERE
					kd_report = '".$as_report_code."' AND
					id_item_induk = '".$id_item_induk."'
				ORDER BY
					no_urut, id_item";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubDataArusKas($as_report_code, $arr_data[$i]['id_item'], $arr_data[$i]['level'], $adt_from,$as_project_code,$as_office_code,$as_activity_code));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
		
	//==============================================================
	
	function GetReportDataNeracaLajur($as_report_code, $adt_from='', $as_project_code='%', $as_office_code='%', $as_activity_code='%'){
		$adt_from = ($adt_from=='' ? date('d/m/Y') : $adt_from);
		$adt_from = date_format3($adt_from, 'YYYY-MM-DD');
		
		$ls_sql	= "SELECT
						keu_setup_report_item.*, gl.saldo_bulan_lalu, gl.st_pos, gl.db_now, gl.cr_now, gl.total_mutasi_db, 
							gl.total_mutasi_cr, gl.saldo_akhir, gl.db, gl.cr,
					(SELECT COUNT(1) FROM keu_setup_report_item tmp WHERE tmp.id_item_induk = keu_setup_report_item.id_item) numchild,
					(SELECT COUNT(1) FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item) item_coa, 
					0 level
				FROM
					keu_setup_report_item left join
					(
						SELECT
							icoa.id_item,
							keu_coa.st_pos,
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo_bulan_lalu,
							sum(ifnull(gl.db, 0)) db,
							sum(ifnull(gl.cr, 0)) cr,
							sum(ifnull(gl.db_now, 0))db_now,
							sum(ifnull(gl.cr_now, 0))cr_now,
							sum(ifnull(gl.db, 0))+ sum(ifnull(gl.db_now, 0))total_mutasi_db,
							sum(ifnull(gl.cr, 0))+ sum(ifnull(gl.cr_now, 0))total_mutasi_cr,
							sum(
						CASE ifnull(keu_coa.st_pos, 'D')
						WHEN 'D' THEN
							(
								ifnull(gl.db, 0)+ ifnull(gl.db_now, 0)
							)-(
								ifnull(gl.cr, 0)+ ifnull(gl.cr_now, 0)
							)
						ELSE
							(
								ifnull(gl.cr, 0)+ ifnull(gl.cr_now, 0)
							)-(
								ifnull(gl.db, 0)+ ifnull(gl.db_now, 0)
							)
						END
					)saldo_akhir

						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH('".$adt_from."')- 1
										WHEN 0 THEN
											IFNULL(begin_db, 0)
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH('".$adt_from."')- 1
										WHEN 0 THEN
											IFNULL(begin_cr, 0)
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(db12, 0)
										END
									)db_now,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(cr12, 0)
										END
									)cr_now
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."')
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						GROUP BY id_item
					)gl on keu_setup_report_item.id_item = gl.id_item
				WHERE

					keu_setup_report_item.kd_report = '".$as_report_code."' AND
					ifnull(keu_setup_report_item.id_item_induk,'') = '' 

				ORDER BY
					keu_setup_report_item.no_urut, keu_setup_report_item.id_item
					";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubDataNeracaLajur($as_report_code, $arr_data[$i]['id_item'], $arr_data[$i]['level'], $adt_from,$as_project_code,$as_office_code,$as_activity_code));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
	
	function GetReportSubDataNeracaLajur($as_report_code, $id_item_induk='', $ai_level=0, $adt_from='', $as_project_code='%', $as_office_code='%', $as_activity_code='%'){
		if($id_item_induk=='') return NULL;
		$adt_from = ($adt_from=='' ? date('d/m/Y') : $adt_from);
		$adt_from = date_format3($adt_from, 'YYYY-MM-DD');
		
		$ls_sql	= "	SELECT
						keu_setup_report_item.*, gl.saldo_bulan_lalu, gl.st_pos, gl.db_now, gl.cr_now, gl.total_mutasi_db, 
							gl.total_mutasi_cr, gl.saldo_akhir, gl.db, gl.cr,
					(SELECT COUNT(1) FROM keu_setup_report_item tmp WHERE tmp.id_item_induk = keu_setup_report_item.id_item) numchild,
					(SELECT COUNT(1) FROM keu_setup_report_item_coa tmp WHERE tmp.id_item = keu_setup_report_item.id_item) item_coa, 
					".($ai_level+1)." level
					FROM
					keu_setup_report_item left join
					(
						SELECT
							icoa.id_item,
							keu_coa.st_pos,
							sum(
								CASE ifnull(keu_coa.st_pos, 'D')
								WHEN 'D' THEN
									ifnull(gl.db, 0)- ifnull(gl.cr, 0)
								ELSE
									ifnull(gl.cr, 0)- ifnull(gl.db, 0)
								END
							)saldo_bulan_lalu,
							sum(ifnull(gl.db, 0)) db,
							sum(ifnull(gl.cr, 0)) cr,
							sum(ifnull(gl.db_now, 0))db_now,
							sum(ifnull(gl.cr_now, 0))cr_now,
							sum(ifnull(gl.db, 0))+ sum(ifnull(gl.db_now, 0))total_mutasi_db,
							sum(ifnull(gl.cr, 0))+ sum(ifnull(gl.cr_now, 0))total_mutasi_cr,
							sum(
						CASE ifnull(keu_coa.st_pos, 'D')
						WHEN 'D' THEN
							(
								ifnull(gl.db, 0)+ ifnull(gl.db_now, 0)
							)-(
								ifnull(gl.cr, 0)+ ifnull(gl.cr_now, 0)
							)
						ELSE
							(
								ifnull(gl.cr, 0)+ ifnull(gl.cr_now, 0)
							)-(
								ifnull(gl.db, 0)+ ifnull(gl.db_now, 0)
							)
						END
					)saldo_akhir

						FROM
							(
								SELECT
									gl_ledger.code_coa,
									sum(
										CASE MONTH('".$adt_from."')- 1
										WHEN 0 THEN
											IFNULL(begin_db, 0)
										WHEN 1 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(begin_db, 0)+ IFNULL(db1, 0)+ IFNULL(db2, 0)+ IFNULL(db3, 0)+ IFNULL(db4, 0)+ IFNULL(db5, 0)+ IFNULL(db6, 0)+ IFNULL(db7, 0)+ IFNULL(db8, 0)+ IFNULL(db9, 0)+ IFNULL(db10, 0)+ IFNULL(db11, 0)+ IFNULL(db12, 0)
										END
									)db,
									sum(
										CASE MONTH('".$adt_from."')- 1
										WHEN 0 THEN
											IFNULL(begin_cr, 0)
										WHEN 1 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(begin_cr, 0)+ IFNULL(cr1, 0)+ IFNULL(cr2, 0)+ IFNULL(cr3, 0)+ IFNULL(cr4, 0)+ IFNULL(cr5, 0)+ IFNULL(cr6, 0)+ IFNULL(cr7, 0)+ IFNULL(cr8, 0)+ IFNULL(cr9, 0)+ IFNULL(cr10, 0)+ IFNULL(cr11, 0)+ IFNULL(cr12, 0)
										END
									)cr,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(db1, 0)
										WHEN 2 THEN
											IFNULL(db2, 0)
										WHEN 3 THEN
											IFNULL(db3, 0)
										WHEN 4 THEN
											IFNULL(db4, 0)
										WHEN 5 THEN
											IFNULL(db5, 0)
										WHEN 6 THEN
											IFNULL(db6, 0)
										WHEN 7 THEN
											IFNULL(db7, 0)
										WHEN 8 THEN
											IFNULL(db8, 0)
										WHEN 9 THEN
											IFNULL(db9, 0)
										WHEN 10 THEN
											IFNULL(db10, 0)
										WHEN 11 THEN
											IFNULL(db11, 0)
										WHEN 12 THEN
											IFNULL(db12, 0)
										END
									)db_now,
									sum(
										CASE MONTH('".$adt_from."')
										WHEN 1 THEN
											IFNULL(cr1, 0)
										WHEN 2 THEN
											IFNULL(cr2, 0)
										WHEN 3 THEN
											IFNULL(cr3, 0)
										WHEN 4 THEN
											IFNULL(cr4, 0)
										WHEN 5 THEN
											IFNULL(cr5, 0)
										WHEN 6 THEN
											IFNULL(cr6, 0)
										WHEN 7 THEN
											IFNULL(cr7, 0)
										WHEN 8 THEN
											IFNULL(cr8, 0)
										WHEN 9 THEN
											IFNULL(cr9, 0)
										WHEN 10 THEN
											IFNULL(cr10, 0)
										WHEN 11 THEN
											IFNULL(cr11, 0)
										WHEN 12 THEN
											IFNULL(cr12, 0)
										END
									)cr_now
								FROM
									gl_ledger
								WHERE
									YEAR = YEAR('".$adt_from."')
									AND project_code like '".$as_project_code."'
									AND code_office like '".$as_office_code."'
									AND code_activity like '".$as_activity_code."'
								GROUP BY
									code_coa
							)gl,
							keu_coa,
							keu_setup_report_item_coa icoa
						WHERE
							gl.code_coa = keu_coa. CODE
						AND keu_coa.id_coa = icoa.id_coa
						GROUP BY id_item
					)gl on keu_setup_report_item.id_item = gl.id_item
				WHERE
					keu_setup_report_item.kd_report = '".$as_report_code."' AND
					keu_setup_report_item.id_item_induk = '".$id_item_induk."' 

				ORDER BY
					keu_setup_report_item.no_urut, keu_setup_report_item.id_item";
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			$arr_data	= $result->result_array();
			
			$arr_return = array();
			
			for($i=0,$max_i=count($arr_data); $i<$max_i; $i++){
				array_push($arr_return, $arr_data[$i]);
				if(intval($arr_data[$i]['numchild']) > 0){
					$arr_return = array_merge($arr_return, $this->GetReportSubDataNeracaLajur($as_report_code, $arr_data[$i]['id_item'], $arr_data[$i]['level'], $adt_from,$as_project_code,$as_office_code,$as_activity_code));
				}
			}
			
			return $arr_return;
		}else{
			return NULL;
		}
	}
	
}
?>